from django.db import models
from django.contrib.auth.models import AbstractUser
# Create your models here.
class PermissionType(models.Model):
    name = models.CharField('权限类型',max_length=30)

    def __str__(self):
        return self.name
    class Meta:
        db_table = 'peimission_type'
        verbose_name_plural = '权限类型'

class User(AbstractUser):
    #添加额外的类属性
    mobile = models.CharField('手机号',max_length=11)
    role = models.ManyToManyField(PermissionType)
    def __str__(self):
        return self.username
    class Meta:
        db_table = 'user_tb'
        verbose_name = '用户表'
        verbose_name_plural = verbose_name


class Permission(models.Model):
    title = models.CharField('权限名称',max_length=30)
    code_name = models.CharField('权限代码',max_length=300)
    type_name = models.ForeignKey(PermissionType,on_delete=models.CASCADE,verbose_name='权限类型')
    def __str__(self):
        return self.title
    class Meta:
        db_table = 'permission'
        verbose_name_plural = '用户权限'
#用户组
class UserGroup(models.Model):
    name = models.CharField('用户组名',max_length=30)
    user = models.ManyToManyField(User)
    permission = models.ManyToManyField(Permission)
    def __str__(self):
        return self.name
    class Meta:
        db_table = 'user_group'
        verbose_name_plural = '用户组'